/**
 * nghilt
 * 2011/09/16
 *
 * Copyright by N&N.,Inc
 */
package vn.nnn.shogi.quanco;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JApplet;

import at.com.vn.chess.util.RectangleUtils;

import vn.nnn.shogi.board.Board;

/**
 * 
 */
public class QuanTinh extends QuanCo {
	
	static final int UP_LEFT = 0;
	static final int UP_RIGHT = 1;
	static final int DOWN_RIGHT = 2;
	static final int DOWN_LEFT = 3;

	public QuanTinh( Rectangle toaDo, int color, boolean normalDirection, JApplet mainApplet ) {
		super( toaDo, color, normalDirection, mainApplet );	
	}
	
	@Override
	public void setToaDoNhaTu() {
		toaDoNhaTu = new Point( 0, 1 );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#canMove(java.awt.Rectangle, vn.nnn.shogi.quanco.QuanCo, java.util.ArrayList)
	 */
	@Override
	public boolean canMove( Board board, Rectangle destination,
			QuanCo foundedPiece, ArrayList<QuanCo> listQuanCo ) {
		
		if( !RectangleUtils.isTrenMotDuongCheo( toaDo, destination ) ) {
			return false;
		}
		
		if ( biCan( destination, listQuanCo ) ) {
			return false;
		}
		
		return isPassToBasicMoveCondition( board, destination, foundedPiece, listQuanCo );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#upgrade(java.awt.Rectangle, java.util.ArrayList)
	 * Upgrade len ngua rong
	 */
	@Override
	public void upgrade( Rectangle destination, ArrayList<QuanCo> listQuanCo ) {
		QuanTinhPhongCap tinhPhongCap = new QuanTinhPhongCap( destination, color, normalDirection, getMainApplet() );
		int idxChosen = getPositionInList( listQuanCo );
		
		listQuanCo.remove( idxChosen );
		listQuanCo.add( idxChosen, tinhPhongCap );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#canRelease(vn.nnn.shogi.quanco.Board, java.awt.Rectangle, java.util.ArrayList)
	 */
	@Override
	public boolean canRelease( Board board, Rectangle destination,
			ArrayList<QuanCo> listQuanCo ) {
		return isPassToBasicReleaseCondition( board, destination, listQuanCo );
	}
	
	/**
	 * @param destination
	 * @param listQuanCo
	 * @return
	 */
	public boolean biCan( Rectangle destination, ArrayList<QuanCo> listQuanCo ) {
		
		int row = RectangleUtils.convertToRealBoardRow( toaDo );	
		int column = RectangleUtils.convertToRealBoardCol( toaDo );
		
		int nextRow = RectangleUtils.convertToRealBoardRow( destination );
		int nextCol = RectangleUtils.convertToRealBoardCol( destination );

		int tempcoor[] = new int[2];
		boolean can = false;

		int movecase = moveCase( nextRow, nextCol );
		int length = Math.abs( nextRow - row );

		tempcoor[0] = row;
		tempcoor[1] = column;
		for( int i = 0; i < length - 1; i++ ) {
			BishopCoor( tempcoor, movecase );
			for ( int j = 0; j < listQuanCo.size(); j++ ) {
				if ( tempcoor[0] == RectangleUtils.convertToRealBoardRow( listQuanCo.get( j ).toaDo )
						&& tempcoor[1] == RectangleUtils.convertToRealBoardCol( listQuanCo.get( j ).toaDo ) ) {
					can = true;
				}					
			}
		}
		
		return can;
	}

	public int moveCase( int nextRow, int nextCol ) {
		int dRow = nextRow - RectangleUtils.convertToRealBoardRow( toaDo );
		int dCol = nextCol - RectangleUtils.convertToRealBoardCol( toaDo );

		if ( dRow < 0 && dCol < 0 ) {
			return UP_LEFT;
		}

		if ( dRow < 0 && dCol > 0 ) {
			return UP_RIGHT;
		}

		if ( dRow > 0 && dCol > 0 ) {
			return DOWN_RIGHT;
		}

		if ( dRow > 0 && dCol < 0 ) {
			return DOWN_LEFT;
		}

		return 10;
	}

	public boolean BishopCoor( int coor[], int movecase ) {
		boolean moveok = false;
		switch ( movecase ) {
			case UP_LEFT: //up to the left
				coor[0]--;
				coor[1]--;
				break;
			case UP_RIGHT: // up to the right
				coor[0]--;
				coor[1]++;
				break;
			case DOWN_RIGHT: // down to the right
				coor[0]++;
				coor[1]++;
				break;
			case DOWN_LEFT: // down to the left
				coor[0]++;
				coor[1]--;
				break;
		}
		if ( coor[0] >= 0 && coor[0] < 9 )
			if ( coor[1] >= 0 && coor[1] < 9 )
				moveok = true;
		return moveok;
	}
}
